package cn.wolfcode.trip.admin.web.controller;

import cn.wolfcode.trip.admin.web.util.UploadUtil;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/travelCommend")
public class TravelCommendController {

    @Autowired
    private ITravelCommendService travelCommendService;

    //游记推荐列表展示
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") TravelCommendQuery qo){
        model.addAttribute("result", travelCommendService.getPageInfo(qo));
        return "/travelCommend/list";
    }

    //游记推荐的新增和编辑操作
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(TravelCommend travelCommend, MultipartFile pic){

        //封面图片需要上传 然后设置进功游记攻略对象中
        if(pic!=null && pic.getSize()>0){
           String imagePath =  UploadUtil.upload(pic,UploadUtil.IMG_PATH+"upload" );
           travelCommend.setCoverUrl(imagePath);
        }
        travelCommendService.saveOrUpdate(travelCommend);

        return new JSONResult();
    }




}
